#include "myemotionwidget.h"
#include "ui_myemotionwidget.h"
#include <QTableWidgetItem>
#include <QLabel>
#include <QMovie>
#include <QFile>
#include <QDebug>

MyEmotionWidget::MyEmotionWidget(QWidget *parent)
    : QTableWidget(parent)
    , m_tableRow(0)
    , m_tableColumn(0)
    , m_preRow(0)
    , m_preColumn(0)
    , m_maxRow(6)
    , m_emotionSize(QSize(0 , 0))
    , m_emotionMovieSize(QSize(0 , 0))
{
    //loadStyleSheet();
}
MyEmotionWidget::~MyEmotionWidget()
{}

// 直接从文件中获取样式
void MyEmotionWidget::loadStyleSheet()
{
    QFile file(":/Resources/QSS/myemotion.css");
    file.open(QFile::ReadOnly);
    QString strSheet = file.readAll();
    QString styleSheet = this->styleSheet();
    styleSheet += strSheet;
    this->setStyleSheet(styleSheet);
}
// 设置表情窗口的行列数目
void MyEmotionWidget::setRowAndColumn(int row, int column)
{
    m_tableRow = row;
    m_tableColumn = column;
}
// 设置item表情框大小
void MyEmotionWidget::setEmotionSize(QSize emotionSize)
{
    m_emotionSize = emotionSize;
}
// 设置表情movie大小
void MyEmotionWidget::setEmotionMovieSize(QSize emotionMovieSize)
{
    m_emotionMovieSize = emotionMovieSize;
}
// 设置最大行数
void MyEmotionWidget::setMaxRow(int maxRow)
{
    m_maxRow = maxRow;
}
// 设置完参数后，进行初始化
void MyEmotionWidget::initTableWidget()
{
    // 设置无焦点
    this->setFocusPolicy(Qt::NoFocus);
    // 设置不可编辑
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    // 设置行数
    this->setRowCount(m_tableRow);
    // 设置列数
    this->setColumnCount(m_tableColumn);
    // 设置表头不可见并设置表情item框大小
    this->horizontalHeader()->setVisible(false);
    this->horizontalHeader()->setDefaultSectionSize(m_emotionSize.width());

    this->verticalHeader()->setVisible(false);
    this->verticalHeader()->setDefaultSectionSize(m_emotionSize.height());

    // 设置表情窗口的大小，这里行数超过m_maxRow时作了处理，当行数超过给的最大值，则通过滚动 滚动条显示剩余的表情
    if (m_tableRow > m_maxRow)
    {
        this->setFixedHeight(m_emotionSize.height()*m_maxRow+ 2);
        // 这里宽度加30，是因为在这种情况下会tablewidget会显示出滚动条，所以为了显示正常，增加一点宽度
        this->setFixedWidth(m_emotionSize.width()*m_tableColumn + 30);
    }
    else
    {
        this->setFixedHeight(m_emotionSize.height()*m_tableRow + 2);
        this->setFixedWidth(m_emotionSize.width()*m_tableColumn + 2);
    }
}
//添加表情
void MyEmotionWidget::addEmotionItem(QString fileName , QString toolTip)
{
    int row = m_emotionList.size() / this->columnCount();
    int column = m_emotionList.size() % this->columnCount();

    QTableWidgetItem* tableWidgetItem = new QTableWidgetItem;
    tableWidgetItem->setToolTip(toolTip);
    this->setItem(row, column, tableWidgetItem);


    MyEmotionItemWidget* cellWidget = new MyEmotionItemWidget(fileName);
    cellWidget->setContentsMargins(3, 3, 3, 3);
    // 设置鼠标悬浮时显示蓝色边框;
    cellWidget->setStyleSheet("QLabel:hover{border: 1px solid rgb(111, 156, 207);\
                                color: rgb(2, 65, 132);\
                                background: rgba(255, 255, 255, 200);}");
    //connect(cellWidget, &MyEmotionItemWidget::emotionClicked, this, &MyEmotionWidget::addEmotion);
    connect(cellWidget, SIGNAL(emotionClicked(QString)), this, SLOT(addEmotion(QString)));
    this->setCellWidget(row, column, cellWidget);
    m_emotionList.push_back(fileName);
}
//发送选择
    void MyEmotionWidget::addEmotion(QString filename)
    {
        //qDebug()<<"选中"<<emotionNum;
        emit signalItemClicked(filename);
    }

// 因为MyEmotionWidget作为子部件来使用，故nativeEvent接收不到事件，这里由外部调用传入鼠标在父部件的位置
void MyEmotionWidget::showDynamicEmotion(QPoint cursorPos)
{
    // 当前传入的为鼠标在父控件上的位置，所以还需要减去tableWidget在父控件上的位置，其结果为鼠标在tableWidget的位置
    QPoint point = cursorPos - this->pos();
    QTableWidgetItem* tableWidgetItem = this->itemAt(point);
    if (tableWidgetItem != NULL)
    {
        //如果获取到的item不为空说明此刻鼠标所在的区域在tableWidget的某个item中，
        //并且如果鼠标从一个表情移动到一个其他未设置item的单元格或者移出tableWidget区域
        //在同一个item中移动则不做任何操作;
        if (m_preRow != tableWidgetItem->row() || m_preColumn != tableWidgetItem->column())
        {
            QLabel* preEmotion = (QLabel*)this->cellWidget(m_preRow, m_preColumn);
            if (preEmotion != NULL)
            {
                //恢复原来的margin
                preEmotion->setContentsMargins(3, 3, 3, 3);
                QMovie* preMoive = preEmotion->movie();
                preMoive->jumpToFrame(0);
                preMoive->stop();
            }

            QLabel* curEmotion = (QLabel*)this->cellWidget(tableWidgetItem->row(), tableWidgetItem->column());
            if (curEmotion != NULL)
            {
                // 当鼠标悬浮在item上时修改margin值达到表情切换效果，见下图（在鼠标从一个表情滑到另一个表情时)
                curEmotion->setContentsMargins(4, 2, 2, 4);
                QMovie* curMoive = curEmotion->movie();
                curMoive->start();
                m_preRow = tableWidgetItem->row();
                m_preColumn = tableWidgetItem->column();
            }
        }
    }
    else
    {
        //如果获取到的item为空说明此刻鼠标所在的区域不在tableWidget的items中，
        //并且如果鼠标刚从一个动态表情移动到一个其他未设置item的单元格或者移出tableWidget区域，
        //则根据上一个表情的位置获取到item并停止正在动态显示的表情，并将m_preRow、m_preColumn置为-1，
        //以免下次再次进入到此分支进行重复操作;
        QTableWidgetItem* tableWidgetItem = this->item(m_preRow, m_preColumn);
        if (tableWidgetItem != NULL)
        {
            QLabel* preEmotion = (QLabel*)this->cellWidget(m_preRow, m_preColumn);
            if (preEmotion != NULL)
            {
                //恢复原来的margin
                preEmotion->setContentsMargins(3, 3, 3, 3);
                QMovie* preMoive = preEmotion->movie();
                preMoive->jumpToFrame(0);
                preMoive->stop();
                m_preRow = -1;
                m_preColumn = -1;
            }
        }
    }
}

/*

//初始化表情
void MyEmotionWidget::initEmotion()
{

    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
        ui->tableWidget->setFocusPolicy(Qt::NoFocus);
        ui->tableWidget->setMouseTracking(true);
        ui->tableWidget->installEventFilter(this);
        this->installEventFilter(this);

        QString path = ":/QQexpression/%1.gif";
        for (int i = 0; i < 10; i++)
        {
            addEmotionItem(path.arg(i + 1));
        }

}
*/
MyEmotionItemWidget::MyEmotionItemWidget(QString fileName)
    : QLabel(NULL)
{
    //首先构造函数中进行初始化;
    QMovie* iconMovie = new QMovie;
    iconMovie->setFileName(fileName);
    this->setMovie(iconMovie);
    this->setMargin(4);
    //为了动态图片正常显示，（不加上start图片不显示，不加上stop图片一种处于动态效果）
    iconMovie->start();
    iconMovie->stop();
    m_fielname=fileName;
}
// 鼠标进入Label事件
void MyEmotionItemWidget::enterEvent(QEvent* event)
{
    QMovie* movie = this->movie();
    movie->start();
    return QWidget::enterEvent(event);
    //return __super::enterEvent(event);
}
// 鼠标离开Label事件
void MyEmotionItemWidget::leaveEvent(QEvent* event)
{
    QMovie* movie = this->movie();
    movie->jumpToFrame(0);
    movie->stop();
    return QWidget::leaveEvent(event);
}
void MyEmotionItemWidget::mousePressEvent(QMouseEvent *event)
{
    //qDebug()<<m_index;
    emit emotionClicked(m_fielname);  // 当鼠标单击的时候，发出点击信号
}

